<!-- 内容主体区域 -->
<link rel="stylesheet" href="layui/css/layui.css">
<style>
    .layui-table-cell .layui-form-checkbox[lay-skin="primary"]{
        top: 50%;
        transform: translateY(-50%);
    }
</style>
<div style="padding: 15px;">
    <div class="layui-tab">
        <div class="layui-tab-content">

            <div class="layui-input-inline" style="">
                <div class="layui-input-inline">
                    <button id="tongguoAll" class="layui-btn layui-btn-normal">审核通过</button>
                </div>
            </div>
            <div class="layui-input-inline" style="">
                <div class="layui-input-inline">
                    <button id="add" class="layui-btn layui-btn-normal">新建田径选手</button>
                </div>
            </div>
            <table class="layui-hide" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
            </script>
            <script type="text/html" id="show-img">
                <img src="http://svcsa.org/uploads/{{d.PhotoSrc}}" alt="图片名">
            </script>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            <script type="text/html" id="orderListBar">
                {{#  if(d.Individual==0){ }}
                集体项目
                {{#  } else { }}
                个人项目
                {{#  } }}
            </script>
            <script type="text/html" id="shenhe">
                {{#  if(d.Flag==0){ }}
                <a class="layui-btn layui-btn layui-btn-xs" lay-event="tongguo">通过</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="bohui">驳回</a>
                {{#  } else { }}
                <a class="layui-btn layui-btn-disabled layui-btn-xs">已通过</a>
                {{#  } }}
            </script>
            <script type="text/html" id="Flag">
                {{#  if(d.Flag==0){ }}
                申请中
                {{#  } else { }}
                通过申请
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="layui/layui.js"></script>
<script>
    function tongguo(ids, passed) {
        $.ajax({
            url: 'http://svcsa.org/ctfcplayer/passapp',
            type: 'Post',
            dataType: 'json',
            data: {
                PlayerIDs: ids,
                Passed: passed
            },
            header: {
                Accept: 'application/json'
            },
            success: function (data) {
                layer.msg('操作成功', {
                    icon: 1,
                    time: 1200
                }, function () {
                    location.reload();
                });
            },
            error: function (data) {
                console.log(data)
            }
        });
    }

    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#test',
            url: 'http://svcsa.org/CTFCPlayer',
            parseData: function (res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": '', //解析提示文本
                    "count": res.totalcount, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            method: 'get',
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                ,limitName: 'pagesize' //每页数据量的参数名，默认：limit
            },
            title: '用户数据表',
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left',
                    width: 90
                }, {
                    field: 'ID',
                    title: 'ID',
                    width: 80,
                    fixed: 'left',
                    unresize: true,
                    sort: true,
                }, {
                    field: 'Name',
                    title: '选手/团队名称',
                    width: 150,
                }, {
                    field: 'Leader',
                    title: '负责人姓名',
                    // sort: true,
                    width: 120,
                },
                //     {
                //     field: 'PhotoSrc',
                //     title: '选手照片',
                //     templet: '#show-img',
                //     width: 150,
                // },
                    {
                    field: 'Individual',
                    title: '项目类型',
                    templet: '#orderListBar',
                    width: 120,
                },
                //     {
                //     field: 'Email',
                //     title: '联系方式-电子邮箱',
                //     width: 160,
                // },
                    {
                    field: 'Tel',
                    title: '联系方式-手机',
                    width: 120,
                }, {
                    field: 'Wechat',
                    title: '联系方式-微信',
                    width: 120,
                }, {
                    field: 'Track',
                    title: '赛道',
                    width: 100,
                }, {
                    field: 'MatchName',
                    title: '田径项目',
                    width: 200,
                }
                    , {
                    field: 'Flag',
                    title: '申请比赛',
                    templet: '#Flag',
                    width: 150,
                }, {
                    title: '审核',
                    width: 130,
                    toolbar: '#shenhe'
                }, {
                    title: '操作',
                    width: 130,
                    toolbar: '#barDemo'
                }]
            ],
            limit: 20,
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页

            },
        });
        $("#add").click(function () {
            var vv = layer.open({
                type: 2,
                title: '新建田径选手',
                shadeClose: true,
                shade: 0.8,
                area: ['100%', '100%'],
                content: 'ctfc_player_add.html',
                success: function (layero, index) {
                    // layer.iframeAuto(index);
                },
                end: function () {
                    location.reload();
                }
            });
            layer.full(vv)
        })
        $('#tongguoAll').click(function () {
            var mycars = ''
            var data = table.checkStatus('test').data; //idTest 即为基础参数 id 对应的值
            if (data.length === 0) {
                layer.alert('请至少选择一名运动员')
            } else {
                for (var i = 0; i < data.length; i++) {
                    if (i === data.length - 1) {
                        if (data[i].Flag === 0) {
                            mycars += data[i].ID
                        }
                    } else {
                        if (data[i].Flag === 0) {
                            mycars += data[i].ID + ','
                        }
                    }
                }
            }
            tongguo(mycars, 1)
        })
        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'edit') { //查看
                console.log(obj.data);
                var title = "修改田径选手"
                if (data.ID != null) {
                    title = "修改田径选手"
                }
                var vv = layer.open({
                    type: 2,
                    title: title,
                    shadeClose: true,
                    shade: 0.8,
                    area: ['100%', '100%'],
                    content: 'ctfc_player_add.html',
                    success: function (layero, index) {
                        // layer.iframeAuto(index);
                        var body = layer.getChildFrame('body', index);
                        body.find('#Name').val(data.Name)
                        body.find('#Leader').val(data.Leader)
                        body.find('#Individual').val(data.Individual)
                        body.find('#Email').val(data.Email)
                        body.find('#Tel').val(data.Tel)
                        body.find('#Wechat').val(data.Wechat)
                        body.find('#Track').val(data.Track)
                        body.find('#ID').val(data.ID)
                        body.find('#EventID').val(data.MatchID)
//                        body.find('#Flag').val(data.Flag)
                        body.find('#PhotoSrc').val(data.PhotoSrc)
                        body.find('#demo1').attr("src", "http://svcsa.org/uploads/" + data.PhotoSrc)
                        body.find('#img_url').val(data.PhotoSrc)

                    },
                    end: function () {
                        location.reload();
                    }
                });
                layer.full(vv)
            } else if (layEvent === 'del') {
                layer.confirm('确认删除', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    $.ajax({
                        url: 'http://svcsa.org/CTFCPlayer/' + data.ID,
                        //url:"http://svcsa.org/bbcompetition",
                        type: 'delete',
                        dataType: 'json',
                        header: {
                            Accept: 'application/json'
                        },
                        success: function (data) {
                            layer.msg('删除成功', {
                                icon: 1,
                                time: 1200
                            }, function () {
                                location.reload();
                            });
                        },
                        error: function (data) {
                            console.log(data)
                        }
                    });

                }, function () {
                });
            } else if (layEvent === 'tongguo') {
                layer.confirm('确认要审核通过吗？', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    tongguo(data.ID, 1)
                }, function () {
                });
            } else if (layEvent === 'bohui') {
                layer.confirm('确认要驳回吗？', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    tongguo(data.ID, 0)
                }, function () {
                });
            }
        });
    });
</script>